import { createSlice } from '@reduxjs/toolkit'
import { setStorage, getStorage, removeAllStorage } from "../../utils/storage";
const USER_STORAGE_KEY = 'USER_DATA'
const Token = 'Token'
const userSlice = createSlice({
   name: 'user',
   initialState: {
      userData: getStorage(USER_STORAGE_KEY) || {},
      token: getStorage(Token) || ''
   },
   reducers: {
      saveUserData: (state, payload) => {
         const { Authorization, data} = payload.payload
         state.userData = data
         state.token = Authorization
         setStorage(USER_STORAGE_KEY,data)
         setStorage(Token,Authorization)
      },
      logOut: state => {
         removeAllStorage()
         state.userData = {}
      },
   }
})

export default userSlice.reducer
export const { saveUserData,logOut } = userSlice.actions
